package com.icity.util;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.ocean.rawsdk.ApiExecutor;
import com.alibaba.ocean.rawsdk.client.exception.OceanException;
import com.icity.entity.ActiveUserInfo;
import com.icity.entity.LaunchInfo;
import com.icity.entity.NewUserInfo;
import com.icity.mapper.UmStatisticMapper;
import com.umeng.uapp.param.UmengUappGetActiveUsersParam;
import com.umeng.uapp.param.UmengUappGetActiveUsersResult;
import com.umeng.uapp.param.UmengUappGetChannelDataParam;
import com.umeng.uapp.param.UmengUappGetChannelDataResult;
import com.umeng.uapp.param.UmengUappGetDurationsParam;
import com.umeng.uapp.param.UmengUappGetDurationsResult;
import com.umeng.uapp.param.UmengUappGetLaunchesParam;
import com.umeng.uapp.param.UmengUappGetLaunchesResult;
import com.umeng.uapp.param.UmengUappGetNewUsersParam;
import com.umeng.uapp.param.UmengUappGetNewUsersResult;

@Component
public class Scheduler {

	@Autowired
	private UmStatisticMapper umStatisticMapper;

	@Scheduled(cron = "0 39 18 ? * *")
	public void test() throws SQLException, ParseException {
		ApiExecutor apiExecutor = new ApiExecutor("5764644", "7xCp4NJ1mV6");
		apiExecutor.setServerHost("gateway.open.umeng.com");
		Scheduler testOpenAPI = new Scheduler();
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for (int i = 105; i <= 145; i++) {
			String dateStr = DateUtils.getSpecifiedDayBefore(sdf.format(date), i);

			String launchInfoStr = testOpenAPI.umengUappGetLaunches(apiExecutor, dateStr);
			System.out.println(launchInfoStr);
			JSONObject jsonLaunch = JSONObject.parseObject(launchInfoStr);
			LaunchInfo launchInfo = new LaunchInfo();
			JSONArray jsonArrayLaunch = jsonLaunch.getJSONArray("launchInfo");
			JSONObject jsonObjectLaunch = jsonArrayLaunch.getJSONObject(0);
			launchInfo.setDate(jsonObjectLaunch.getString("date"));
			launchInfo.setValue(jsonObjectLaunch.getInteger("value"));
			umStatisticMapper.insertLaunchinfo(launchInfo);

			String newUserInfoStr = testOpenAPI.umengUappGetNewUsers(apiExecutor, dateStr);
			System.out.println(newUserInfoStr);
			JSONObject jsonNewUser = JSONObject.parseObject(newUserInfoStr);
			NewUserInfo newUserInfo = new NewUserInfo();
			JSONArray jsonArrayNewUser = jsonNewUser.getJSONArray("newUserInfo");
			JSONObject jsonObjectNewUser = jsonArrayNewUser.getJSONObject(0);
			newUserInfo.setDate(jsonObjectNewUser.getString("date"));
			newUserInfo.setValue(jsonObjectNewUser.getInteger("value"));
			umStatisticMapper.insertNewUserInfo(newUserInfo);

			String activeUserInfoStr = testOpenAPI.umengUappGetActiveUsers(apiExecutor, dateStr);
			System.out.println(activeUserInfoStr);
			JSONObject jsonActive = JSONObject.parseObject(activeUserInfoStr);
			ActiveUserInfo activeUserInfo = new ActiveUserInfo();
			JSONArray jsonArrayActive = jsonActive.getJSONArray("activeUserInfo");
			JSONObject jsonObjectActive = jsonArrayActive.getJSONObject(0);
			activeUserInfo.setDate(jsonObjectActive.getString("date"));
			activeUserInfo.setValue(jsonObjectActive.getInteger("value"));
			umStatisticMapper.insertActiveUserInfo(activeUserInfo);

			try {
				Thread.currentThread().sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
 	}


	// 每日的启动次数
	public String umengUappGetLaunches(ApiExecutor apiExecutor, String date) {
		UmengUappGetLaunchesParam param = new UmengUappGetLaunchesParam();
		// 测试环境只支持http
		// param.getOceanRequestPolicy().setUseHttps(false);
		param.setAppkey("5b7a29f1f43e482a4a0000b5");
		param.setStartDate(date);
		param.setEndDate(date);
		param.setPeriodType("daily");

		try {
			UmengUappGetLaunchesResult result = apiExecutor.execute(param);
			return JSONObject.toJSONString(result);
		} catch (OceanException e) {
			return "errorCode=" + e.getErrorCode() + ", errorMessage=" + e.getErrorMessage();
		}
	}

	// 每日新增用户数
	public String umengUappGetNewUsers(ApiExecutor apiExecutor, String date) {
		UmengUappGetNewUsersParam param = new UmengUappGetNewUsersParam();
		// 测试环境只支持http
		// param.getOceanRequestPolicy().setUseHttps(false);
		param.setAppkey("5b7a29f1f43e482a4a0000b5");
		param.setStartDate(date);
		param.setEndDate(date);
		param.setPeriodType("daily");

		try {
			UmengUappGetNewUsersResult result = apiExecutor.execute(param);
			return JSONObject.toJSONString(result);
		} catch (OceanException e) {
			return "errorCode=" + e.getErrorCode() + ", errorMessage=" + e.getErrorMessage();
		}
	}

	// 每日日活数
	public String umengUappGetActiveUsers(ApiExecutor apiExecutor, String date) {
		UmengUappGetActiveUsersParam param = new UmengUappGetActiveUsersParam();
		// 测试环境只支持http
		// param.getOceanRequestPolicy().setUseHttps(false);
		param.setAppkey("5b7a29f1f43e482a4a0000b5");
		param.setStartDate(date);
		param.setEndDate(date);
		param.setPeriodType("daily");

		try {
			UmengUappGetActiveUsersResult result = apiExecutor.execute(param);
			return JSONObject.toJSONString(result);
		} catch (OceanException e) {
			return "errorCode=" + e.getErrorCode() + ", errorMessage=" + e.getErrorMessage();
		}
	}

	// 获取每日使用时间
	public String umengUappGetDurations(ApiExecutor apiExecutor, String date) {
		UmengUappGetDurationsParam param = new UmengUappGetDurationsParam();
		// 测试环境只支持http
		// param.getOceanRequestPolicy().setUseHttps(false);
		param.setAppkey("5b7a29f1f43e482a4a0000b5");
		param.setDate(date);
		param.setStatType("daily");
		// param.setChannel("App%20Store");
		// param.setVersion("1.0.0");

		try {
			UmengUappGetDurationsResult result = apiExecutor.execute(param);
			return JSONObject.toJSONString(result);
		} catch (OceanException e) {
			return "errorCode=" + e.getErrorCode() + ", errorMessage=" + e.getErrorMessage();
		}
	}

	// 查询每日各渠道的用户数
	public String umengUappGetChannelData(ApiExecutor apiExecutor) {
		UmengUappGetChannelDataParam param = new UmengUappGetChannelDataParam();
		// 测试环境只支持http
		// param.getOceanRequestPolicy().setUseHttps(false);
		param.setAppkey("5b7a29f1f43e482a4a0000b5");
		param.setDate("2018-12-01");

		try {
			UmengUappGetChannelDataResult result = apiExecutor.execute(param);
			return JSONObject.toJSONString(result);
		} catch (OceanException e) {
			return "errorCode=" + e.getErrorCode() + ", errorMessage=" + e.getErrorMessage();
		}
	}
}
